package com.example.demo.es.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.example.demo.es.entity.Person;
import com.example.demo.es.repository.EsRepository;
import com.example.demo.es.service.EsService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;


@Service
@Slf4j
public class EsServiceImpl implements EsService {
    private final IndexCoordinates personIndex = IndexCoordinates.of("person");
    @Autowired
    private EsRepository esRepository;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    private List<Person> persons;

    @PostConstruct
    public void createDemoData() {
        persons = Collections.synchronizedList(new ArrayList<>());

        Stream.iterate(0, i -> i + 1)
                .limit(1000)
                .parallel()
                .forEach((i) -> {
                    Person person = new Person();
                    person.setId(UUID.fastUUID().toString());
                    person.setName("小明" + RandomUtil.randomString(30));
                    person.setMoney(RandomUtil.randomDouble());
                    person.setAddress("地址是" + RandomUtil.randomStringUpper(40));
                    person.setAge(RandomUtil.randomInt(10, 90));
                    persons.add(person);
                });
        log.info("初始化demo数据");
    }

    @Override
    public void saveRestSingle() {
        esRepository.saveAll(persons);
    }

    @Override
    public void delete() {
        String delete = elasticsearchRestTemplate.delete("0", personIndex);
        log.info(delete);
    }

    @Override
    public void update() {
        UpdateQuery updateQuery = UpdateQuery.builder("0").build();
        UpdateResponse update = elasticsearchRestTemplate.update(updateQuery, personIndex);
        log.info(update.getResult().toString());

    }

    @Override
    public void find() {
        Person person = elasticsearchRestTemplate.get("0", Person.class, personIndex);
        System.out.println(person);
    }

    @Override
    public void findList() {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 72))
                .should(QueryBuilders.matchQuery("name", "小明"));
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(1, 10))
                .withSort(SortBuilders.fieldSort("age"))
                .build();
        SearchHits<Person> search = elasticsearchRestTemplate.search(nativeSearchQuery, Person.class);
        System.out.println(search);
    }

}
